import apiConst from '@/common/js/apiConst'
import common from '@/api/common'
import request from '@/utils/request'

export default {
  notifySuccess: (message) => {
    common.notify('成功', 'success', message)
  },
  notifyWarning: (message) => {
    common.notify('警告', 'warning', message)
  },
  notifyError: (message) => {
    common.notify('异常提示', 'error', message)
  },
  queryAllProduct: () => {
    return common.get(apiConst.API.GET_ALL_PRODUCT_LIST)
  },

  queryVersionsByproduct: (product) => {
    return common.get(`${apiConst.API.GET_VERSIONS_BY_PRODUCT}/${product}`)
  },

  queryFotaServers: () => {
    return common.get(apiConst.API.GET_FOTA_SERVERS)
  },

  queryPkgList: (pageReq) => {
    return common.post(apiConst.API.GET_PKG_LIST, pageReq)
  },

  existPkgByProductAndVersion: (pkgInfo) => {
    return common.post(apiConst.API.EXIST_PKG_BY_PRODUCT_AND_NAME, pkgInfo)
  },

  insertPkg: (pkgInfo) => {
    return common.post(apiConst.API.INSERT_PKG, pkgInfo)
  },

  updatePkg: (pkgInfo) => {
    return common.put(apiConst.API.UPDATE_PKG, pkgInfo)
  },

  deletePkg: (id) => {
    return common.delete(`${apiConst.API.DELETE_PKG}/${id}`)
  },

  queryPlanInfo: (pageReq) => {
    return common.post(apiConst.API.GET_PLAN_INFO_LIST, pageReq)
  },

  queryPlanById: (id) => {
    return common.get(`${apiConst.API.GET_PLAN_INFO_LIST}/${id}`)
  },

  existPlanByName: (planName) => {
    return common.get(`${apiConst.API.EXIST_PLAN_BY_NAME}/${planName}`)
  },

  insertPlan: (pkgInfo) => {
    return common.post(apiConst.API.INSERT_PLAN, pkgInfo)
  },

  updatePlan: (pkgInfo) => {
    return common.put(apiConst.API.UPDATE_PLAN, pkgInfo)
  },

  deletePlan: (id) => {
    return common.delete(`${apiConst.API.DELETE_PLAN}/${id}`)
  },

  queryLogList: (pageReq) => {
    return common.post(apiConst.API.GET_LOG_LIST, pageReq)
  },
  exportLog: (filterBy) => {
    return request({
      url: apiConst.API.EXPORT_LOG,
      method: 'post',
      data: filterBy,
      responseType: 'blob'
    })
  },
  deleteLog: (id) => {
    return common.delete(`${apiConst.API.DELETE_LOG}/${id}`)
  },
  queryPlanNames(subStr) {
    return common.get(`${apiConst.API.GET_PLAN_NAME_LIST}/${subStr}`)
  },
  queryProducts(subStr) {
    return common.get(`${apiConst.API.GET_PRODUCT_LIST}/${subStr}`)
  },
  queryVersions(product, subStr) {
    return common.get(`${apiConst.API.GET_VERSIONS_LIST}/${product}/${subStr}`)
  },
  querySyslog() {
    return common.get(apiConst.API.GET_SYS_LOG)
  },
  downloadSysLog(name, server) {
    if (server === undefined || server.key === 'management') {
      return request({
        url: `${apiConst.API.DOWNLOAD_SYS_LOG}/${name}`,
        method: 'get',
        responseType: 'blob'
      })
    }
    return request({
      url: `http://${server.domain}${server.contextPath}${apiConst.API.DOWNLOAD_SYS_LOG}/${name}`,
      method: 'get',
      responseType: 'blob'
    })
  }
}
